package main

import (
	"context"
	pb "go_grpc/client/proto"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
)

type ClientTokenAuth struct {
}

func (c ClientTokenAuth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
	return map[string]string{
		"appid":  "wulei",
		"appkey": "123123",
	}, nil
}

func (c ClientTokenAuth) RequireTransportSecurity() bool {
	return false
}

//func main() {
//	creds, _ := credentials.NewClientTLSFromFile("D:\\goWork\\src\\go_grpc\\key\\test.pem",
//		"*.leistudy.com")
//	//连接到 server 端，此处禁用安全传输，没有加密和验证
//	//connect, err := grpc.Dial("127.0.0.1:9090", grpc.WithTransportCredentials(insecure.NewCredentials()))
//	connect, err := grpc.Dial("127.0.0.1:9090", grpc.WithTransportCredentials(creds))
//	defer connect.Close()
//	if err != nil {
//		log.Fatal("连接失败")
//		return
//	}
//	//建立连接
//	client := pb.NewSayHelloClient(connect)
//	//执行 rpc 调用
//	response, _ := client.SayHello(context.Background(), &pb.HelloRequest{
//		RequestName: " wulei",
//	})
//	println(response.GetResponseMsg())
//}

func main() {
	//连接到 server 端，此处禁用安全传输，没有加密和验证
	var opts []grpc.DialOption
	opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
	opts = append(opts, grpc.WithPerRPCCredentials(new(ClientTokenAuth)))
	connect, err := grpc.Dial("127.0.0.1:9090", opts...)
	defer connect.Close()
	if err != nil {
		log.Fatal("连接失败")
		return
	}
	//建立连接
	client := pb.NewSayHelloClient(connect)
	//执行 rpc 调用
	response, err := client.SayHello(context.Background(), &pb.HelloRequest{
		RequestName: " wulei",
	})
	if err != nil {
		println(err.Error())
	}
	println(response.GetResponseMsg())
}
